






var menuItem = function (parent, options) {
    this.options = options;
    this.html = $(`<div class="MenuItem">
        <span><i class="fa fa-link" aria-hidden="true"></i></span>
        <div class="children"></div>
    </div>`);
    this.name = this.html.find('span');
    this.name.append(options.name);
    this.childrenBox = this.html.find('.children');
    this.children = [];

    if (options.children) {
        for (let i = 0; i < options.children.length; i++) {
            let element = options.children[i];
            let item = new menuItem(parent, element);
            this.children.push(item);
            this.childrenBox.append(item.html);
        };
    };
    this.name.click(() => {
        parent.active(this);
    });
};
menuItem.prototype.active = function () {
    this.name.addClass('active');
};
menuItem.prototype.unActive = function () {
    this.name.removeClass('active');
    for (let i = 0; i < this.children.length; i++) {
        const element = this.children[i];
        element.unActive();
    };
};








var menu = function () {
    this.html = $('<div id="Menu"></div>');
    this.event = {
        click: function () { },
    };
    this.children = [];
};
menu.prototype.onClick = function (event) {
    this.event.click = event;
};

menu.prototype.active = function (item) {
    for (let i = 0; i < this.children.length; i++) {
        const element = this.children[i];
        element.unActive();
    };
    item.active();
    this.event.click(item.options);

};

menu.prototype.set = function (data) {
    this.html.empty();
    for (let i = 0; i < data.length; i++) {
        let element = data[i];

        if (element.type == 'link') {
            let item = new menuItem(this, element);
            this.children.push(item);
            this.html.append(item.html);
        } else {
            this.html.append('<div class="menuTag">' + element.name + '</div>');
        };
    };
};

export default menu;
